package com.cande.loki.exercise.design;

import java.util.Observable;
import java.util.Observer;

/**
 * @BelongsProject: loki
 * @BelongsPackage: com.cande.loki.exercise.design
 * @Author: WANGAOFEI
 * @CreateTime: 2023-10-26  16:54
 * @Description:  观察者模式
 * @Version: 1.0
 */
public class ObserverTest extends Observable {

    public static void main(String[] args) {
        ObserverA a = new ObserverA();
        ObserverTest sub = new ObserverTest();
        sub.addObserver(a);
        sub.set(2);
    }

    public int state;

    public void set(int param){
        state = param;
        setChanged();
        notifyObservers();
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}

 class ObserverA implements Observer{

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("getgetget"+((ObserverTest)o).getState());
    }
}
